package ask.speedy.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.util.Log;

/**
 * kelas untuk melakukan download file dari suatu URL
 * @author asus
 *
 */
public class RequestServer {
	
	/**
	 * mengecek apakah suatu URL 'ada' atau tidak
	 * @param URLName url yang akan di cek
	 * @return true jika url tersebut ada, false jika tidak
	 */
	private static boolean exists(String URLName){
	    try {
	      HttpURLConnection.setFollowRedirects(false);
	      HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
	      con.setRequestMethod("HEAD");
	      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
	      
	    }
	    catch (Exception e) {
	       e.printStackTrace();
	       return false;
	    }
	  }
	
	/**
	 * melakukan download file dari suatu URL file
	 * @param myURL
	 * @param fileName
	 * @param extension
	 * @return true jika url yang akan diakses 'ada' dan file dapat di download
	 * false jika url yang akan diakses tidak ditemukan
	 */
	public static boolean DownloadFromURL(String myURL, String localFilePath, String localFileName) {
		if (exists(myURL)) {
			return Download(myURL, localFilePath, localFileName);
		} else {
			return false;
		}
	}
	
	/**
	 * melakukan download file
	 * @param urlSource url dari file yang akan didownload 
	 * @param fileName nama file yang akan di simpan di lokal direktori
	 * @param ext ektensi file yang akan disimpan di lokal direktori
	 */
	private static boolean Download(String urlSource, String localFilePath, String localFileName) {
		boolean retval = false;
		
    	try {
    		URL url = new URL(urlSource);
    		File theDir = new File(localFilePath);
			if (!theDir.exists()) {
				theDir.mkdir();
			}
    		
            File file = new File(localFilePath, localFileName);
            if (!file.exists()) {
            	file.createNewFile();
            }
            
            //Open a connection to that URL and create InputStreams to read from the ucon
            URLConnection ucon = url.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            //Read bytes to the Buffer until there is nothing more to read(-1).
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
            }

            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();
            
            retval = true;
            
        } catch (IOException e) {
                Log.d("ncret", "Error: " + e);
        }
        
        return retval;
	}
	
}
